package single.shop.web.freemarker.method;

import java.io.StringWriter;
import java.io.Writer;

import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

/**
 * 模板处理器
 * 
 * @author Yinyt
 *
 */
public class FreeMarkerProcessor
{

	private FreeMarkerConfigurer freemarkerConfig;

	public String process(String template, Object data) throws Exception
	{
		String value = null;
		Writer fw = null;
		try
		{
			fw = new StringWriter();
			freemarkerConfig.getConfiguration().getTemplate(template).process(data, fw);
			fw.flush();
			value = fw.toString();
		} catch (Exception e)
		{
			throw e;
		} finally
		{
			if (fw != null)
				fw.close();
		}
		return value;
	}

	public FreeMarkerConfigurer getFreemarkerConfig()
	{
		return freemarkerConfig;
	}

	public void setFreemarkerConfig(FreeMarkerConfigurer freemarkerConfig)
	{
		this.freemarkerConfig = freemarkerConfig;
	}
}
